package com.kaoshi.servefw.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaoshi.common.config.Result;
import com.kaoshi.servefw.domain.Dwellers;
import com.kaoshi.servefw.domain.Health;
import com.kaoshi.servefw.domain.Stables;
import com.kaoshi.servefw.service.DwellersService;
import com.kaoshi.servefw.service.HealthService;
import com.kaoshi.servefw.service.MfilyService;
import com.kaoshi.servefw.service.StablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/b/stables")
public class StablesController {
    @Autowired
    private StablesService stablesService;
    @Autowired
    private HealthService healthService;
    @Autowired
    private DwellersService dwellersService;

    @Autowired
    private MfilyService mfilyService;

    @RequestMapping("/findPage")
    public Result pageList(Integer pageNum, Integer pageSize, @RequestBody Stables stables) {
        Page page = stablesService.findPage(pageNum, pageSize, stables);
        return Result.ok(page);
    }
//    @PostMapping("/add")
//    public Result add(@RequestBody Stables stables){
//        stablesService.save(stables);
//        return Result.ok();
//    }
//    @PostMapping("/adddew")
//    public Result adddew(@RequestBody Dwellers dwellers){
//        dwellersService.save(dwellers);
//        return Result.ok();
//    }

    @RequestMapping("/findPageSr")
    public Result findPageSr(Integer pageNum, Integer pageSize, @RequestBody Stables stables) {
        Page page = stablesService.findPageSr(pageNum, pageSize, stables);
        return Result.ok(page);
    }


    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        Stables stables = stablesService.findById(id);
        return Result.ok(stables);
    }

    @PostMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
        System.out.println("id:" + id);
        System.out.println("status:" + status);
        Stables stables = stablesService.findById(id);
        stables.setStatus(status);
        if (status == 1) {
            System.out.println("asddasasssasdsadwadwsadsfghhffffffffffyffyffyuffyufyufufuyfyufuyf");
        }
        //获取当前时间为审核时间
        stables.setAudit(new Date());
        stablesService.updateById(stables);
        return Result.ok();
    }

    //添加
    @PostMapping("/update")
    public Result update(@RequestBody Stables stables) {
        stables.setOverrule(stables.getOverrule());
        stablesService.updateById(stables);
        return Result.ok();
    }

    //完成服务
    @PostMapping("/add/{id}")
    public Result add(@RequestBody Health health, @PathVariable Integer id) {
        //1.新增健康信息
        healthService.save(health);
        System.out.println("--------------------------------------------------------------------" + id);
//        /2.修改stables表中hid字段
        Stables byId = stablesService.findById(id);
        byId.setHid(health.getId());
        //获取当前时间为完成时间
        byId.setFinish(new Date());
        stablesService.updateById(byId);
        return Result.ok();
    }

    //取消服务
    @PostMapping("/canupdate")
    public Result canupdate(@RequestBody Stables stables) {
        stables.setCancel(stables.getCancel());
        //获取当前时间为取消时间
        stables.setLifted(new Date());
        stablesService.updateById(stables);
        return Result.ok();
    }

    @RequestMapping("/addDwellers")
    public Result addDwellers(@RequestBody Dwellers dwellers) {
        try {
            System.out.println(dwellers);
            // 1. 保存 Dwellers 对象并获取其生成的 id
            Dwellers dwellersServiceById = dwellersService.getById(dwellers.getId());
            dwellers.setName(dwellersServiceById.getName());
            dwellers.setAge(dwellersServiceById.getAge());
            dwellersService.save(dwellers);
            return Result.ok(dwellers);
        } catch (Exception e) {
            // 处理异常情况
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/addStales/{zbid}")
    public Result addStales(@PathVariable Integer zbid,@RequestBody Stables stables) {
        System.out.println("stables11111111111111111111111111111111111111111111111111111111111111111111111111111:" + zbid);
//        List<Dwellers> list = dwellersService.list();
//        int lastId = 0;
//        for (int i = 0; i < list.size(); i++) {
//            if (!list.isEmpty()) {
//                Dwellers lastDweller = list.get(list.size() - 1);
//                lastId = lastDweller.getId();
//            }
//        }
//        System.out.println("最后一个元素的ID: " + lastId);
        // 其他属性可以根据需要设置
        stables.setDewid(zbid);
        stables.setSid(2);
        stables.setBooking(new Date());
        stables.setSubmi(new Date());
        stables.setStatus(1);
        stables.setIsdel(0);
        stables.setReviewers("admin");
        // 3. 保存 Stables 对象
        stablesService.save(stables);
        return Result.ok();
    }
    @RequestMapping("/findUpdate/{id}")
    public Result findUpdate(@PathVariable Integer id){
        Stables stables = stablesService.findById(id);
        return Result.ok(stables);
    }
    @RequestMapping("/UpdateStales")
    public Result UpdateStales(@RequestBody Stables stables){
        stablesService.updateById(stables);
        return Result.ok();
    }
    @RequestMapping("/UpdateDwellers/{dwid}")
    public Result UpdateDwellers(@PathVariable Integer dwid, @RequestBody Dwellers dwellers) {
        dwellers.setId(dwid);
        dwellersService.updateById(dwellers);
        return Result.ok();
    }
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("服务列表", "UTF-8").replaceAll("/+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        //2.查询要导出的数据
        List<Stables> studentList= stablesService.list();

        //3.使用EasyExcel完成导出
        EasyExcel.write(response.getOutputStream())
                .head(Stables.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("服务列表")
                .doWrite(studentList);

    }
}



